<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="visible"
    :width="screenWidth"
    :fullscreen="mobile === 'mobile'"
    :title="dialogAddOrEdit.title"
    draggable
    close-icon="CloseBold"
    @close="closeDialog"
  >
    <el-form ref="addOrEditFormRef" v-loading="loading" :model="formData" :rules="rules" label-width="130" size="">
      <el-row justify="start" class="d_left">
        <el-col :span="itemSpan" class="">
          <el-form-item label="指挥部名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入指挥部名称" />
          </el-form-item>
        </el-col>

        <el-col :span="itemSpan" class="d_left">
          <el-form-item label="指挥者身份证号码" prop="cardno">
            <el-input v-model="formData.cardno" placeholder="请输入身份证号码" />
          </el-form-item>
        </el-col>

        <el-col :span="itemSpan" class="">
          <el-form-item label="指挥者姓名" prop="leader">
            <el-input v-model="formData.leader" placeholder="请输入姓名" />
          </el-form-item>
        </el-col>

        <el-col :span="itemSpan" class="d_left">
          <el-form-item label="指挥者手机号码" prop="mobile">
            <el-input v-model="formData.mobile" placeholder="请输入手机号码" />
          </el-form-item>
        </el-col>

        <el-col v-if="!dialogAddOrEdit.id" :span="itemSpan" class="">
          <el-form-item label="密码" prop="password">
            <el-input v-model="formData.password" type="password" placeholder="请输入密码" />
          </el-form-item>
        </el-col>

        <el-col v-if="!dialogAddOrEdit.id" :span="itemSpan" class="d_left">
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input v-model="formData.confirmPassword" type="password" placeholder="再次输入确认密码" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{ dialogAddOrEdit.footBtn }}</el-button>
        <el-button @click="closeDialog()">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, nextTick, computed } from "vue";
import { addCommander, editCommander, getCommanderInfo } from "@/api/constructor/project/index.ts";
import { CommanderInfoType } from "@/api/constructor/project/types";
import { getDicts } from "@/store/modules/dict";

import { useScreen } from "@/store/modules/screen";

const screenStore = useScreen();
const mobile = computed(() => screenStore.device);
const screenWidth = ref(900);
const itemSpan = ref(12);

function initScreen() {
  if (mobile.value === "mobile") {
    screenWidth.value = screenStore.screenWidth;
    itemSpan.value = 24;
  } else {
    screenWidth.value = 900;
    itemSpan.value = 12;
  }
}

const props = defineProps({
  dialogAddOrEdit: {
    type: Object,
    default: () => ({
      visible: false,
      title: "新增指挥部信息",
      footBtn: "新增",
      id: 0,
    }),
  },
});

const emit = defineEmits(["sendClose"]);

const loading = ref(false);
let visible = ref(props.dialogAddOrEdit.visible);
const formData = reactive<CommanderInfoType>({});
const addOrEditFormRef = ref(ElForm);

const rules = reactive<FormRules<CommanderInfoType>>({
  name: [{ required: true, message: "请输入指挥部名称", trigger: "blur" }],
  leader: [{ required: true, message: "请输入指挥者名称", trigger: "blur" }],
  mobile: [
    { required: true, message: "请输入指挥者手机号码", trigger: "blur" },
    {
      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
      message: "请输入正确的手机号码",
      trigger: "blur",
    },
  ],
  cardno: [
    { required: true, message: "请输入指挥者身份证号码", trigger: "blur" },
    {
      pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
      message: "请输入正确的身份证号码",
      trigger: "blur",
    },
  ],
  password: [{ required: true, message: "请输入指挥者密码", trigger: "blur" }],
  confirmPassword: [
    { required: true, message: "请再次输入密码", trigger: "blur" },
    { required: true, trigger: "blur", validator: confirmPasswordBlur },
  ],
});

function confirmPasswordBlur(rule: any, value: any, callback: any) {
  if (value === undefined || value === "") {
    callback(new Error("请再次输入密码"));
  } else {
    if (value != formData.password) {
      callback(new Error("两次密码不一致"));
    } else {
      callback();
    }
  }
}

onMounted(async () => {
  initScreen();

  let tempFormData;
  if (props.dialogAddOrEdit.id) {
    loading.value = true;
    tempFormData = await getCommanderInfo(props.dialogAddOrEdit.id);
    tempFormData = tempFormData.data;
    Object.assign(formData, tempFormData);
    formData.id = props.dialogAddOrEdit.id;
    loading.value = false;
    tempFormData = null;
  }
});

/* 发送当前弹窗关闭 */
function sendClose(query) {
  emit("sendClose", query);
}

/**
 * 关闭弹窗
 */
function closeDialog(query) {
  visible.value = false;
  sendClose(query);
  resetForm();
}

/**
 * 禁用日期
 */
function disabledDate(time) {
  if (formData.startDate) {
    return formData.startDate > time.getTime();
  }
}

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();

  formData.id = undefined;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        editCommander(id, formData)
          .then((resp) => {
            if (resp.success === true) {
              ElMessage.success(resp.msg);
              closeDialog(true);
            } else {
              ElMessage.warning(resp.msg);
            }
          })
          .finally(() => (loading.value = false));
      } else {
        addCommander(formData)
          .then((resp) => {
            if (resp.success === true) {
              ElMessage.success(resp.msg);
              closeDialog(true);
            } else {
              ElMessage.warning(resp.msg);
            }
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}
const BankTypeTypes = getDicts("bankTypeTypes"); //银行类别
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 0px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}
.dialog_footer {
  // display: flex;
  // justify-content: center;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
